//
//  StrategyDetailController.m
//  XhlTrip
//
//  Created by dllo on 15/8/23.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "StrategyDetailController.h"
#import "LeaonWorking.h"
#import "StrategyDetailCell.h"
#import "StrategyModel.h"
#import "PlanViewController.h"
#import "TopicViewController.h"
#import "TripViewController.h"
#import "MBProgressHUD.h"

@interface StrategyDetailController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *tableArr;
@end

@implementation StrategyDetailController

-  (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArr = [NSMutableArray array];
    }
    return self;
}

- (void)passValue:(NSString *)str
{
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    hud.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:hud];
    hud.labelText = @"正在加载";
    [hud show:YES];
    
    /* 这里面有好多数据 */
    NSString *ss = [NSString stringWithFormat:@"%@%@%@", @"https://chanyouji.com/api/destinations/",str, @".json"];
    NSString *nn = [NSString stringWithFormat:@"%@%@", @"strategy", str];
    [LeaonWorking netWorkingHanderGetGataWithUrl:ss pathname:nn resultBlock:^(id result) {
        [self.tableArr addObjectsFromArray:result];
        
        [_tableView reloadData];

        [hud removeFromSuperview];
        [hud release];
    }];


}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    /* 创建tableview */
    [self createTableView];
    
}



- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    _tableView.rowHeight = 300 * Height;
    
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    StrategyDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    if (cell == nil) {
        cell = [[[StrategyDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"] autorelease];
    }
     cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    /* 和上一个controller的model是一样的 */
    StrategyModel *model = [[[StrategyModel alloc] init] autorelease];
    [model setValuesForKeysWithDictionary:self.tableArr[indexPath.row]];
    cell.strDetModel = model;
    
    [cell.planButton addTarget:self action:@selector(planButton:) forControlEvents:UIControlEventTouchUpInside];
    [cell.planImage addTarget:self action:@selector(planButton:) forControlEvents:UIControlEventTouchUpInside];
    cell.planImage.tag = indexPath.row + 1;
    cell.planButton.tag = indexPath.row +1;
    
    [cell.topicButton addTarget:self action:@selector(topicButton:) forControlEvents:UIControlEventTouchUpInside];
    [cell.topicImage addTarget:self action:@selector(topicButton:) forControlEvents:UIControlEventTouchUpInside];
    cell.topicImage.tag = indexPath.row + 2;
    cell.topicButton.tag = indexPath.row + 2;
    
    cell.tripButton.tag = indexPath.row + 3;
    cell.tripImage.tag = indexPath.row + 3;
    [cell.tripButton addTarget:self action:@selector(tripButton:) forControlEvents:UIControlEventTouchUpInside];
    [cell.tripImage addTarget:self action:@selector(tripButton:) forControlEvents:UIControlEventTouchUpInside];
    
    
    return cell;
}


/* 以下为进入不同controller的点击事件 */
- (void)tripButton:(UIButton *)bu
{
    TripViewController *trip = [[TripViewController alloc] init];
    [trip passVvalue:[self.tableArr[bu.tag - 3] objectForKey:@"id"]];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"旅行地";
    self.navigationItem.backBarButtonItem = backItem;
    [backItem release];

    [self.navigationController pushViewController:trip animated:YES];
    [trip release];
}

- (void)planButton:(UIButton *)sender
{
    PlanViewController *plan = [[PlanViewController alloc] init];
    [plan passId:[self.tableArr[sender.tag - 1] objectForKey:@"id"]];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"行程";
    self.navigationItem.backBarButtonItem = backItem;
    [backItem release];

    [self.navigationController pushViewController:plan animated:YES];
    [plan release];
}


- (void)topicButton:(UIButton *)button
{
    TopicViewController *topic = [[TopicViewController alloc] init];
    [topic passV:[self.tableArr[button.tag - 2] objectForKey:@"id"]];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"专题";
    self.navigationItem.backBarButtonItem = backItem;
    [backItem release];

    [self.navigationController pushViewController:topic animated:YES];
    [topic release];
    
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
- (void)dealloc
{
    [_tableView release];
    [_tableArr release];
    [super dealloc];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
